// 项目在地图中的标记
/*
* by - huxy
* time: 2021-07-23
* dec: 1: drawLable   根基经纬度高程信息，和文本信息绘制 label 和billboard
*/
// import libgif from 'libgif.js'

export default class PositioningLabels {
    constructor() {
        this.DataSource = null;
        this.superGif = null;
        this.entitiesId = [];
    }
    init() {
        this.entitiesId = [];
        viewer.scene.globe.depthTestAgainstTerrain = false;
        // viewer.dataSources.removeAll();
        this.DataSource = null;
        this.DataSource = new Cesium.CustomDataSource('LablesEntities');
        viewer.dataSources.add(this.DataSource);
        const img = document.createElement('img');
        img.src = './img/project.gif'
        img.style.visibility = 'hidde'
        img.style.width = '50px'
        img.style.height = '50px'
        img.setAttribute('id', 'imggif')
        document.body.appendChild(img);
        let gifimg = document.getElementById('imggif')
        this.superGif = new SuperGif({
            gif: gifimg
        });
        return new Promise((resolve, reject) => {
            this.superGif.load(() => {
                resolve()
            })

        })
    }

    drawLable(lon, lat, h, option) {
        const id = option.id;
        this.entitiesId.push(id)
        viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(lon, lat, h),
            id: id,
            label: {
                text: option.name,
                color: Cesium.Color.fromCssColorString('#fff'),
                font: 'normal 16px MicroSoft YaHei',
                showBackground: true,
                outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
                outlineWidth: 2.0,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                scale: 1,
                pixelOffset: new Cesium.Cartesian2(0.0, -1.0),
                pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 50, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 20000.0),
            },
            billboard: {
                name: option.name,
                image: new Cesium.CallbackProperty(() => {
                    return this.superGif.get_canvas().toDataURL("image/png");
                }, false),
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                scale: 0.3,
            }
        })
    }
    hideSource() {
        for (let i = 0; i < this.entitiesId.length; i++) {
            const ent = viewer.entities.getById(this.entitiesId[i]) || null
            ent && (ent.show = false)
        }
    }
    showSource() {
        for (let i = 0; i < this.entitiesId.length; i++) {
            const ent = viewer.entities.getById(this.entitiesId[i]) || null
            ent && (ent.show = true)
        }
    }
}